package com.wskj.service.asyc.impl;

import com.wskj.common.config.JPushConfig;
import com.wskj.common.config.TencentConfig;
import com.wskj.common.utils.common.JPushUtils;
import com.wskj.common.utils.tencent.PushUtils;
import com.wskj.service.asyc.IAsyncService;
import com.wskj.system.domain.SysNotice;
import com.wskj.system.mapper.SysNoticeMapper;
import com.wskj.system.service.ISysCommonService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 协议管理Service业务层处理
 *
 * @author wsdz
 * @date 2022-08-18
 */
@Service
@Slf4j
@AllArgsConstructor(onConstructor_ = {@Lazy})
public class AsyncServiceImpl implements IAsyncService {

    private final SysNoticeMapper sysNoticeMapper;
    private final ISysCommonService iSysCommonService;

    @Override
    @Async
    public void saveNotice(SysNotice sysNotice, Map<String, String> extrasMap, String... alias) {
        try {
            Thread.sleep(5000); //休眠5s
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        /*switch (NoticeTypeClassifyEnum.getEnumByValue(sysNotice.getNoticeTypeClassify())){
            case RECOMMEND_FRIEND:
                //  推送用户-详情
                extrasMap.put("pushFlag","1");
                sysNotice.setPushFlag("1");
                break;
            case DYNAMICS_COMMENT:
                // 动态 穿透
                extrasMap.put("pushFlag","2");
                sysNotice.setPushFlag("2");
                break;
            case APPLY_INVITE:
            case FROM_CANCEL_INVITE:
                // 邀约-被邀请方 穿透
                extrasMap.put("pushFlag","3");
                sysNotice.setPushFlag("3");
                break;
            case ACCEPT_INVITE:
            case REFUSE_INVITE:
            case BE_CANCEL_INVITE:
            case REACH_INVITE:
            case SUCCESS_INVITE:
                // 邀约-邀请方 穿透
                extrasMap.put("pushFlag","4");
                sysNotice.setPushFlag("4");
                break;
            case SUCCESS_SHOP_APPLY:
            case FAIL_SHOP_APPLY:
            case SUCCESS_PARTNERS_APPLY:
            case FAIL_PARTNERS_APPLY:
            case SUCCESS_WISE_APPLY:
            case FAIL_WISE_APPLY:
                // 申请合作
                extrasMap.put("pushFlag","6");
                sysNotice.setPushFlag("6");
                break;
        }*/
        sysNoticeMapper.insert(sysNotice);
        JPushConfig jPushConfig = new JPushConfig();
        jPushConfig.setAppKey(iSysCommonService.getById(1000076L).getCommonValue());
        jPushConfig.setMasterSecret(iSysCommonService.getById(1000077L).getCommonValue());
        jPushConfig.setLiveTime(iSysCommonService.getById(1000078L).getCommonValue());
        JPushUtils.sendPushWithCallback(jPushConfig, sysNotice.getNoticeTitle(), sysNotice.getNoticeContent(), extrasMap, alias);
        log.info("移动推送-数据处理结束...");
    }

    @Override
    @Async
    public void saveNotice(SysNotice sysNotice, String title, String content, String customContent, Long userId, Integer flag, String[]... intent) {
        try {
            Thread.sleep(1000); //休眠5s
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sysNoticeMapper.insert(sysNotice);
        TencentConfig tencentConfig = new TencentConfig();
        PushUtils.pushTagIosAndAndroidUser(tencentConfig, title, content, customContent, userId, intent);
        log.info("移动推送-数据正在处理...");
    }

    @Override
    @Async
    public void saveSysNotice(String title, String content) {
        // 推送用户
        TencentConfig tencentConfig = new TencentConfig();
        PushUtils.pushAllIosAndAndroidUser(tencentConfig, title, content);
    }
}
